﻿using System.Windows;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Controls;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Views
{
    /// <summary>
    /// Interaction logic for TabItemUser.xaml
    /// </summary>
    public partial class TabItemUserView : IUserView
    {
        readonly IUserService userService;
        private ParvisUserListControl listControl;
        private ParvisUserDetailsControl detailsControl;

        public TabItemUserView(IUserService service)
        {
            userService = service;

            InitializeComponent();
            Loaded += TabItemUserView_Loaded;
        }

        private void TabItemUserView_Loaded(object sender, RoutedEventArgs e)
        {
            const bool includeInactiveUsers = true;

            listControl = new ParvisUserListControl();
            detailsControl = new ParvisUserDetailsControl(userService);
            stackPanelContent.Children.Clear();
            stackPanelContent.Children.Add(listControl);
            stackPanelContent.Children.Add(detailsControl);

            listControl.Model = userService.GetUsers(includeInactiveUsers);
            listControl.SelectedUserChanged += listControl_SelectedUserChanged;
        }

        private void listControl_SelectedUserChanged(object sender, RoutedEventArgs e)
        {
            detailsControl.Model = listControl.SelectedItem;
        }

        #region IUserView Members

        public UserPresentationModel Model
        {
            get { return DataContext as UserPresentationModel; }
            set { DataContext = value; }
        }

        #endregion
    }
}
